/*
 * Copyright (C) 2012 Johan Boris Iantila jiantila@gmail.com && http://www.facebook.com/johanboris.iantila
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mg.daloji.antandroyd.mediaplayer;

import java.io.IOException;

import mg.daloji.antandroyd.enums.EStatePlayer;

import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.util.Log;

/**
 * A subclass of android.media.MediaPlayer which provides methods for
 * state-management, data-source management, etc.
 * @author johan iantila 
 */
public class AntandroydPlayer extends android.media.MediaPlayer {
  
	private EStatePlayer  mState;
    private Context mcontext;
   
    public void setStreamStation(String streamStation) {
    	Uri myUri = Uri.parse(streamStation);
        try {
            setDataSource(mcontext,myUri);
            setState(EStatePlayer.CREATED);
        }
        catch (Exception e) {
            Log.e("Antandroyd", "setDataSource failed");
            setState(EStatePlayer.ERROR);
        }
    }
 
    /**
     * Instantiates a StatefulMediaPlayer object.
     */
    public AntandroydPlayer() {
        super();
        setState(EStatePlayer.CREATED);
    }
 
   
    public AntandroydPlayer(Context context,String url) {
        super();
        this.mcontext=context;
        this.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            setDataSource(url);
            setState(EStatePlayer.CREATED);
        }
        catch (Exception e) {
            Log.e("Antandroyd", "setDataSourceFailed");
            setState(EStatePlayer.ERROR);
        }
    }
 
    @Override
    public void reset() {
        super.reset();
        this.mState = EStatePlayer.EMPTY;
    }
 
    @Override
    public void start() {
        super.start();
        setState(EStatePlayer.STARTED);
    }
 
    @Override
    public void pause() {
 
        super.pause();
        setState(EStatePlayer.PAUSED);
 
    }
 
    @Override
    public void stop() {
        super.stop();
        setState(EStatePlayer.STOPPED);
    }
 
    @Override
    public void release() {
        super.release();
        setState(EStatePlayer.EMPTY);
    }
 
    @Override
    public void prepare() throws IOException, IllegalStateException {
        super.prepare();
        setState(EStatePlayer.PREPARED);
    }
 
    @Override
    public void prepareAsync() throws IllegalStateException {
        super.prepareAsync();
        setState(EStatePlayer.PREPARED);
    }
 
    public EStatePlayer getState() {
        return mState;
    }
 
    /**
     * @param state the state to set
     */
    public void setState(EStatePlayer state) {
        this.mState = state;
    }
 
    public boolean isCreated() {
        return (mState == EStatePlayer.CREATED);
    }
 
    public boolean isEmpty() {
        return (mState == EStatePlayer.EMPTY);
    }
 
    public boolean isStopped() {
        return (mState == EStatePlayer.STOPPED);
    }
 
    public boolean isStarted() {
        return (mState == EStatePlayer.STARTED || this.isPlaying());
    }
 
    public boolean isPaused() {
        return (mState == EStatePlayer.PAUSED);
    }
 
    public boolean isPrepared() {
        return (mState == EStatePlayer.PREPARED);
    }
   }